//
//  SIMeiTuanRefreshView.swift
//  SinaImiate
//
//  Created by xiaoming on 16/7/9.
//  Copyright © 2016年 GoodJob. All rights reserved.
//

import UIKit

class SIMeiTuanRefreshView: SIRefreshView {

    ///建筑物
    @IBOutlet weak var buildingIconView: UIImageView!
    ///地球
    @IBOutlet weak var earthIconView: UIImageView!

    ///袋鼠
    @IBOutlet weak var kangarooIconView: UIImageView!
    
    override var parentViewHeight: CGFloat {
        didSet {
            
            print("父视图高度 \(parentViewHeight)")
            
            if parentViewHeight < 23 {
                return
            }
            
            var scale: CGFloat
            
            if parentViewHeight > 126 {
                scale = 1
            } else {
                
                scale = 1 - ((126 - parentViewHeight) / (126 - 23))
                
            }
            
            kangarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
            
            
        }
        
    }
    
    override func awakeFromNib() {
        
        //建筑物动画
        let building1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let building2 = #imageLiteral(resourceName: "icon_building_loading_2")
        
        buildingIconView.image = UIImage.animatedImage(with: [building1, building2], duration: 0.5)
        
        ///地球动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = -2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 3
        
        anim.isRemovedOnCompletion = false
        
        earthIconView.layer.add(anim, forKey: nil)
        
        ///袋鼠动画
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        
        let x = self.bounds.width * 0.5
        let y = self.bounds.height - 23
        
        kangarooIconView.center = CGPoint(x: x, y: y)
        
        kangarooIconView.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
        
        let kangaroo1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kangaroo2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        
        kangarooIconView.image = UIImage.animatedImage(with: [kangaroo1, kangaroo2], duration: 0.25)
        
        
    }
    
}
